package com.itheima.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest4 {

    @Autowired
    private UserMapper userMapper;

    // 投影+排序
    // SELECT id,nick_name,age FROM tb_user ORDER BY age DESC
    @Test
    public void test01() throws Exception {
        // 创建查询对象
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(User::getId, User::getNickName, User::getAge); // SELECT id,nick_name,age \
        wrapper.orderByDesc(User::getAge);// ORDER BY age DESC

        List<User> list = userMapper.selectList(wrapper);
        System.out.println(list);
    }

    // 分组统计（普通语法）
    // SELECT nick_name,COUNT(*) AS total  FROM tb_user GROUP BY nick_name HAVING total > 1
    @Test
    public void test02() throws Exception {
        // 创建查询对象
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("nick_name,COUNT(*) AS total "); // SELECT nick_name,COUNT(*) AS total
        wrapper.groupBy("nick_name");// GROUP BY nick_name
        wrapper.having("total > 1 ");// HAVING total > 1

        List<Map<String, Object>> list = userMapper.selectMaps(wrapper);

        System.out.println(list);

    }
}

